﻿namespace AnalyStock.DataManage;

internal class CommDataMethod<T> where T : class, new()
{
    /// <summary>
    ///     通用数据处理泛型模型，从数据库提取数据，从在线提取数据，提取并保存数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="args">stockCode,startDate,endDate,tradeDate </param>
    /// <returns></returns>

    //获取在线数据，如果正常获取则返回，否则返回为空集合    
    internal static async Task<IList<T>> GetDataOnlineAsync(params string[] args)
    {
        return await CommDataHelper<T>.GetDataOnlineAsync(args)
                                      .WithCapturaException();
    }

    // 获取在线的数据集合，同时返回是否获取成功的标志   
    internal static async Task<(bool IsSuccees, IList<T> IList)>
        IsGetDataOnlineAsync(string ProcessName, params string[] args)
    {
        try
        {
            var result = await CommDataHelper<T>.GetDataOnlineAsync(args)
                                                .ConfigureAwait(false);
            if (result.IsNullOrEmpty())
            {
                throw new Exception("no data was downloaded...");
            }

            return (true, result);
        }
        catch (Exception ex)
        {
            ErrorLogPool.LogErrorMessage(ProcessName, null, args[0] ?? null, ex.Message);
        }

        return (false, null);
    }

    //根据需要判断，获取在线或在库数据
    internal static async Task<IList<T>> GetDataOnlineOrDbsetAsync(bool isOnline, params string[] args)
    {
        return isOnline
            ? await CommDataHelper<T>.GetDataOnlineAsync(args)
                                     .WithCapturaException()
            : await CommDataHelper<T>.GetDataOnDbSetAsync(args)
                                     .WithCapturaException();
    }

    //异步获取数据库数据
    internal static async Task<IList<T>> GetDataOnDbSetAsync(params string[] args)
    {
        return await CommDataHelper<T>.GetDataOnDbSetAsync(args)
                                      .WithCapturaException();
    }

    // 异步下载在线数据，并且及时保存到数据库文件    
    internal static async ValueTask DownloadToDbSetAsync(params string[] args)
    {
        //获取在线数据文件
        var listResult = await CommDataHelper<T>.GetDataOnlineAsync(args)
                                                .WithCapturaException();
        //没有记录，抛出异常放弃执行
        if (listResult.IsNullOrEmpty())
        {
            "获取在线数据错误...!".ErrorMessage();
        }
        else
        {
            //保存到数据库文件
            var intResult = await CommDataHelper<T>.SaveDataToDbSetAsync(listResult, args?[0] ?? null)
                                                   .WithCapturaException();
            $"数据处理完成，在线获取数据{listResult.Count}条，更新数据库添加:{intResult}条记录！".NotifyMessage();
        }

    }

    /// <summary>
    ///     自选股保存、删除的处理方法,T类型限制为 SeleStockOfSelf
    /// </summary>
    /// <param name="args"></param>
    /// <returns></returns>
    public static async ValueTask UpdateSeleStockAsync(IList<SelectStock> list)
    {
        _ = await CommDataHelper<SelectStock>.SaveDataToDbSetAsync(list)
                                             .WithCapturaException();
        if (list.Count > 1)
        {
            "后台数据更新完成...".NotifyMessage();
        }
        else
        {
            $"{list.FirstOrDefault().Name}:已更新完成...".NotifyMessage();
        }
    }

    public static async ValueTask DelSeleStockAsync(IList<SelectStock> list)
    {
        var result = await CommDataHelper<SelectStock>.DeleDataOnDbSetAsync(list)
                                                      .WithCapturaException();
        if (result == 0)
        {
            "该股票不在自选股中...".ErrorMessage();
        }
        else
        {
            $"{list.FirstOrDefault().Name}:已从自选股中删除...".NotifyMessage();
        }
    }

    //保存概念分类
    public static async ValueTask<int> SaveConceptionClassifyAsync(IList<Conception> list, params string[] args)
    {

        var result = await CommDataHelper<Conception>.SaveDataToDbSetAsync(list, args[0])
                                                     .WithCapturaException();
        if (result == 0)
        {
            "该分类已经在概念中...".ErrorMessage();
        }
        else
        {
            $"{args[0]}概念分类添加完成...".NotifyMessage();
        }
        return result;

    }

    public static async ValueTask<int> SaveStockNoteAsync(IList<StockNote> list)
    {
        var result = await CommDataHelper<StockNote>.SaveDataToDbSetAsync(list)
                                                    .WithCapturaException();
        if (result == 0)
        {
            "该股概念注释已修改完毕...".ErrorMessage();
        }
        else
        {
            $"该股概念注释添加完成...".NotifyMessage();
        }
        return result;
    }

}